package com.ketayao.commandparser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.ketayao.excel.helper.ExcelExportHelper;

/** 
 * @author 	<a href="mailto:ketayao@gmail.com">ketayao</a>
 * @since   2014年2月12日 上午10:39:38 
 */
public class ParseFailure {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ParseFailure parser = new ParseFailure();
		//String path = "E:\\workspace_smart\\command-parser\\src\\test\\resources\\MMLTaskResult_CreateAt_2014-02-08091134.txt";
		//String path = "E:\\git-repository-dev\\command-parser\\src\\test\\resources\\多文档.txt";
		String path = args[0];
		try {
			parser.handle(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void handle(String path) {
		File file = new File(path);
		
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		String commandIndex = "命令-----.*";
		
		List<List<String>> units = new ArrayList<List<String>>();
		List<String> lines = new ArrayList<String>();
		String line = null;
		boolean failureCommand = false;
		boolean isCommandStart = false;
		try {
			while ((line = reader.readLine()) != null) {
				if (line.equals("==========失败命令==========")) {
					failureCommand = true;
					continue;
				}
				
				if (line.matches("=+")) {
					failureCommand = false;
					continue;
				}
				
				if (failureCommand == true) {
					if (line.matches(commandIndex) && isCommandStart == false) {
						lines.add(line);
						isCommandStart = true;
						continue;
					} 
					
					if (isCommandStart) {
						lines.add(line);
					}
					
					if (line.matches(" *") && isCommandStart) {
						units.add(lines);
						lines = new ArrayList<String>();
						isCommandStart = false;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		handleExcel(units);
	}

	/**
	 * @param units
	 */
	private void handleExcel(List<List<String>> units) {
		ExcelExportHelper excelExportHelper = new ExcelExportHelper(); 
		excelExportHelper.createRow();
		
		excelExportHelper.setCellHeader(0, "命令");
		excelExportHelper.setCellHeader(1, "网元");
		excelExportHelper.setCellHeader(2, "报文");
		
		for (List<String> lines : units) {
			excelExportHelper.createRow();
			
			String line0 = lines.get(0);
			excelExportHelper.setCell(0, line0.substring(7, line0.length() - 2));
			
			String line1 = lines.get(1); 
			excelExportHelper.setCell(1, line1.split(":")[1].trim());
			
			StringBuilder builder = new StringBuilder();
			String line2 = lines.get(2);
			builder.append(line2.split(":")[1].trim());
			for (int i = 3; i < lines.size(); i++) {
				builder.append(lines.get(i));
			}
			excelExportHelper.setCell(2, builder.toString());
		}
		
		excelExportHelper.export("d:/" + "失败命令" + ".xls");
	}
}
